package com.agua.enturno.vista.modelos;

import org.apache.wicket.model.IModel;
import org.joda.time.LocalDate;
import org.joda.time.format.DateTimeFormat;


public class LocalDateModel implements IModel<String> {
	
	private static final long serialVersionUID = 1L;
	private static final String DATE_FORMAT = "dd/MM/yyyy";
	
	private IModel<LocalDate> localDateModel;
	
	public LocalDateModel(IModel<LocalDate> localDateModel) {
		this.localDateModel = localDateModel;
	}

	@Override
	public void detach() {
		localDateModel.detach();
	}

	@Override
	public String getObject() {
		
		if (localDateModel.getObject() != null)
			return localDateModel.getObject().toString(DATE_FORMAT);
		else
			return "";
	}

	@Override
	public void setObject(String object) {
		
		try {
			this.localDateModel.setObject(LocalDate.parse(object, DateTimeFormat.forPattern(DATE_FORMAT)));
		} catch (Exception e) {
			this.localDateModel.setObject(null);
		}
	}
	
}
